package web.controller.common;

import common.consts.ContentType;
import common.validation.ValidationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import service.CaptchaService;
import service.ServiceResponse;
import web.Routes;
import web.controller.Base;
import web.req.CaptchaRequest;

/**
 * Created by aceyin on 2015/6/8 0008.
 */
@RestController
@RequestMapping(Routes.Common.CAPTCHA)
public class Captcha extends Base {

    @Autowired
    private CaptchaService captchaService;

    @RequestMapping(method = RequestMethod.GET, produces = ContentType.JSON)
    public ServiceResponse get(CaptchaRequest request) {
        ValidationResult result = request.validate();
        if (!result.isSuccess()) {
            return fail(result.getMessage());
        }

        return captchaService.sendSmsCaptcha(request.getMobile(), request.getUsage());
    }
}
